//
//  firstTouchCell.m
//  EMMApp
//
//  Created by 无常先生 on 2018/8/20.
//  Copyright © 2018年 YiXun. All rights reserved.
//

#import "firstTouchCell.h"
#import "UIView+Extension.h"
#import "UIButton+ImageTitleSpacing.h"
#import "ContactWayModel.h"
#import "FirstTouchTypeModel.h"

@interface firstTouchCell ()
@property (nonatomic,weak) UILabel *flagLable;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UIButton *rightArrow;
@property (nonatomic, weak) UILabel *textField;


@end

@implementation firstTouchCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UILabel *flagView = [[UILabel alloc]init];
    flagView.text = @"*";
    flagView.font = [UIFont systemFontOfSize:13];
    flagView.textColor = [UIColor redColor];
    [self.contentView addSubview:flagView];
    self.flagLable = flagView;
    
    UILabel *titlelable = [[UILabel alloc]init];
    [self.contentView addSubview:titlelable];
    titlelable.text = @"接触方式";
    titlelable.font = [UIFont systemFontOfSize:15];
    self.titleLabel = titlelable;
    
    UIButton *arrowBtn = [[UIButton alloc]init];
    [self.contentView addSubview:arrowBtn];
    arrowBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    self.rightArrow = arrowBtn;
    [arrowBtn addTarget:self action:@selector(selectedTypeClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *textField = [[UILabel alloc]init];
    textField.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:textField];
    textField.font = [UIFont systemFontOfSize:16];
    textField.textColor = [UIColor colorFromHexCode:@"#999999"];
    textField.text = @"请选择 >";
    self.textField = textField;

}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.flagLable.frame = CGRectMake(10, 0, 10, 10);
    self.flagLable.centerY = self.contentView.centerY;
    
    self.titleLabel.frame = CGRectMake(self.flagLable.x + self.flagLable.width, 0, 85, 35);
    self.titleLabel.centerY = self.flagLable.centerY;
    
    self.rightArrow.frame = CGRectMake(self.width - 120, 0, 110, 35);
    self.rightArrow.centerY = self.contentView.centerY;
    
    CGFloat x = self.flagLable.width + self.flagLable.x + 10;
    self.textField.frame = CGRectMake(x, 0, self.contentView.width - (x + 10), self.contentView.height);
}

#pragma mark - methods
- (void)selectedTypeClick:(UIButton *)btn {
    NSMutableArray *aryM = [NSMutableArray array];
    for (FirstTouchTypeModel *model in self.types) {
        [aryM addObject:model.dictionaryName];
    }
    ShowMenuView *menuView = [[ShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) title:@"选项" titleNmae:@"接触方式" numLits:nil items:aryM];
    menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:menuView];
    __block ShowMenuView *weakMenuView = menuView;
    [menuView setTouchBlock:^(NSString *indet,NSString *number) {
        if(indet){
            NSString *text = [NSString stringWithFormat:@"    %@", indet];
            self.textField.text = text;
             self.textField.textColor = [UIColor colorFromHexCode:@"#333333"];
            if (self.selectedTypeCallBack) {
                FirstTouchTypeModel *model = [self.types objectAtIndex:[number integerValue] - 1];
                self.selectedTypeCallBack(model.dictionaryId);
            }
        }
        [weakMenuView removeFromSuperview];
    }];
    
}

- (void)updataUIWithModel:(ContactWayModel *)model indxPath:(NSIndexPath *)index {

    self.rightArrow.userInteractionEnabled = index.section != 0 && index.row == 0;
    
    self.titleLabel.text = index.row == 0 ? @"接触方式" : @"接触日期";
    if (index.section == 0) {
        NSString *name = model.ftContactWayName ? model.ftContactWayName : model.ftContactWayName1;
        NSString *time = model.ftContactTime ? model.ftContactTime : model.ftContactTime1;
        
        NSString *text = index.row == 0 ? name : time;
        self.textField.text = text;
        self.textField.textColor = [UIColor colorFromHexCode:@"#333333"];
    }else {
        FirstTouchTypeModel *typeModel = self.types.lastObject;
        NSString *type = model.ftContactWayName1.length ?  model.ftContactWayName1 : typeModel.dictionaryName;
        
        NSString *text = index.row == 0 ? type : model.ftContactTime1;
        if (text) {
            self.textField.text = text;
             self.textField.textColor = [UIColor colorFromHexCode:@"#333333"];
        }
        if ([self.titleLabel.text isEqualToString:@"接触日期"] && !model.ftContactTime1) {
            self.textField.text = self.nowData;
            self.textField.textColor = [UIColor colorFromHexCode:@"#333333"];
        }
            self.rightArrow.enabled = NO;
    }
    
}


@end
